#ifndef SPAGE_H
#define SPAGE_H

#include "BufferFrame.h"
#include "Record.h"
#include "GlobalStuff.h"
#include "SPSegment.h"

class SPage
{

private:
public:
    struct Header
    {
        unsigned slotCount; // number of used slots
        unsigned firstFreeSlot; // to speed up locating free slots
        unsigned dataStart; // lower end of the data
    };

    struct Slot
    {
enum Type :
        unsigned char { empty, normal, tid };

        unsigned offset; // im Falle von type=tid wird hier die ReferenzPAGE gespeichert
        unsigned length; // im Falle von type=tid wird hier der ReferenzSLOT gespeichert
        bool type;
    };

    SPage(BufferFrame& page, SPSegment& segment);

    SPSegment& segment;
    BufferFrame& page;
    Header& header;

    Slot& getSlot(unsigned slotId);
    char* getGlobalPageStart();
    unsigned getFreeSpaceOnPage();

public:
    static SPage fromExisting(BufferFrame& page, SPSegment& segment);
    static SPage fromNew(BufferFrame& page, SPSegment& segment);

    Record get(unsigned slotId);
    bool clear(unsigned slotId);
    signed insert(const Record& r);
    bool update(unsigned slotId, const Record& r, bool allowOutsourcing);

    BufferFrame& getPage();

};

#endif // SPAGE_H
